<template>
  <div class="box">
    <div class="con">
      <div class="title">选择借用模板</div>
      <div class="scroll" v-loading="loading">
        <div style="width: 4%" title="上一页" @click="prePage">
          <i
            class="el-icon-arrow-left"
            style="font-size: 40px; font-weight: bold; cursor: pointer"
            v-if="current !== 1"
          ></i>
        </div>
        <transition name="el-zoom-in-center">
          <div class="scroll-c" v-show="show">
            <div
              v-for="(item, index) in data"
              :key="index"
              class="item"
              @click="handSelect(item)"
            >
              <div>
                <img
                  :src="require('../../../assets/template.png')"
                  alt=""
                  style="width: 50px; height: 60px"
                />
              </div>
              <div>{{ item.templateName }}</div>
            </div>
          </div>
        </transition>
        <div style="width: 4%" title="下一页" @click="toPage">
          <i
            class="el-icon-arrow-right"
            style="font-size: 40px; font-weight: bold; cursor: pointer"
            v-if="showNext"
          ></i>
        </div>
      </div>
      <div class="bottom">
        <el-button class="btn" @click="next" icon="el-icon-right"
          >不需要模板,自行拿取</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { getBorrowTemplates, seleTem } from "@/common/api";
import { mapState, mapMutations } from "vuex";
export default {
  name: "BorrowTemplate",
  props: {},
  components: {},
  data() {
    return {
      template: [],
      data: [], //当前页数据
      show: true,
      current: 1, //当前页
      totalPage: 0, //总共页
      loading: false,
    };
  },
  computed: {
    showNext() {
      if (this.current >= this.totalPage) {
        return false;
      } else {
        return true;
      }
    },
  },
  watch: {},
  created() {
    this.getBorrowTemplates();
  },
  methods: {
    ...mapMutations(["setTemplateName"]),
    getBorrowTemplates() {
      getBorrowTemplates().then((res) => {
        if (res.success) {
          this.template = res.data;
          this.data = this.template.slice(0, 8);
          this.totalPage = Math.ceil(this.template.length / 8);
        }
      });
    },

    prePage() {
      if (this.current == 1) {
        //已经第一页
        return;
      }
      this.show = !this.show;
      setTimeout(() => {
        this.show = !this.show;
      }, 200);
      this.current -= 1;
      this.data = this.template.slice(
        (this.current - 1) * 8,
        (this.current - 1) * 8 + 8
      );
    },
    toPage() {
      if (this.current >= this.totalPage) {
        //已经最后一页
        return;
      }
      this.show = !this.show;
      setTimeout(() => {
        this.show = !this.show;
      }, 200);
      this.current += 1;
      this.data = this.template.slice(
        (this.current - 1) * 8,
        (this.current - 1) * 8 + 8
      );
    },
    handSelect(e) {
      this.setTemplateName(e.templateName);
      this.loading = true;
      seleTem({ templateName: e.templateName }).then((res) => {
        this.loading = false;
        if (res.success) {
          this.$emit("goStep", 2);
        } else {
          // this.$errorMsg(res.message);
        }
      });
    },
    next() {
      this.setTemplateName("");
      this.$emit("goStep", 2);
    },
  },
  mounted() {},
};
</script>


<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .con {
    width: 70%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2),
      /* 水平偏移 | 垂直偏移 | 模糊半径 | 阴影颜色与透明度 */ 0 6px 20px
        rgba(0, 0, 0, 0.19); /* 可以添加多个阴影效果 */
    border-radius: 14px;
    padding: 20px;
    .scroll {
      display: flex;
      align-items: center;

      .scroll-c {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        width: 92%;

        .item {
          margin: 30px 0px;
          display: flex;
          flex-direction: column;
          align-items: center;
          width: 25%;
          color: #009191;
          font-weight: bold;
          cursor: pointer;
        }
      }
    }
    .title {
      color: #009191;
      font-weight: bold;
      font-size: 22px;
    }
    .bottom {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>
